package com.zmc.coupon.infrastructure.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmc.common.utils.StringUtils;
import com.zmc.coupon.infrastructure.bean.query.CouponSkuQuery;
import com.zmc.coupon.infrastructure.bean.vo.CouponReceiveVo;
import com.zmc.coupon.infrastructure.bean.vo.CouponSkuVo;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zmc.common.utils.PageUtils;
import com.zmc.coupon.infrastructure.dao.CouponSkuDao;
import com.zmc.coupon.infrastructure.entity.CouponSkuEntity;
import com.zmc.coupon.infrastructure.service.CouponSkuService;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author zmc
 */
@Service("couponSpuRelationService")
public class CouponSkuServiceImpl extends ServiceImpl<CouponSkuDao, CouponSkuEntity> implements CouponSkuService {

    @Override
    public PageUtils queryPage(CouponSkuQuery query) {
        return new PageUtils(baseMapper.selectPage(new Page<>(query.getPageNum(), query.getPageSize()), getWrapper(query)));
    }

    @Override
    public List<CouponSkuEntity> queryList(CouponSkuQuery query) {
        return list(getWrapper(query));
    }

    @Override
    public CouponSkuEntity getSpuRelation(CouponReceiveVo vo) {
        LambdaQueryWrapper<CouponSkuEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CouponSkuEntity::getCouponId, vo.getCouponId())
                .eq(CouponSkuEntity::getSkuId, vo.getSkuId());
        return getOne(wrapper);
    }

    @Override
    @Transactional
    public void removeByCouponId(Long couponId) {
        LambdaUpdateWrapper<CouponSkuEntity> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(CouponSkuEntity::getCouponId, couponId);
        remove(wrapper);
    }

    @Override
    public List<CouponSkuEntity> getByCouponIdAndSkuId(List<CouponSkuVo> collect) {
        return baseMapper.getByCouponIdAndSkuId(collect);
    }

    @Override
    public List<CouponSkuEntity> getBySkuIds(List<String> skuIds) {
        LambdaQueryWrapper<CouponSkuEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(CouponSkuEntity::getSkuId, skuIds);
        return list(wrapper);
    }

    @Override
    public List<String> getByCouponId(Long couponId) {
        LambdaQueryWrapper<CouponSkuEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CouponSkuEntity::getCouponId, couponId);
        return list(wrapper).stream().map(CouponSkuEntity::getSkuId).collect(Collectors.toList());
    }

    private LambdaQueryWrapper<CouponSkuEntity> getWrapper(CouponSkuQuery query) {
        LambdaQueryWrapper<CouponSkuEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotEmpty(query.getSkuId()), CouponSkuEntity::getSkuId, query.getSkuId())
                .eq(StringUtils.isNotEmpty(query.getSpuName()), CouponSkuEntity::getSpuName, query.getSpuName())
                .orderByAsc(CouponSkuEntity::getCreateTime);
        return wrapper;
    }
}